<style lang="less">
.hmUserAddr {
    .ivu-modal {
        width: 660px !important;
    }
}
</style>
<template>
    <Modal v-model="show" :title="editTitle" class="hmUserAddr" :mask-closable="false">
        <Form ref="hmUserAddrForm" :model="hmUserAddrForm" :rules="hmUserAddrFormRule" :label-width="100">
            <FormItem label="姓名" prop="receiver">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmUserAddrForm.receiver" placeholder="请输入姓名"/>
            </FormItem>
            <FormItem label="详细地址" prop="addr">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmUserAddrForm.addr" placeholder="请输入详细地址"/>
            </FormItem>
            <FormItem label="手机" prop="mobile">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmUserAddrForm.mobile" placeholder="请输入手机"/>
            </FormItem>
            <FormItem label="是否默认地址 1是" prop="commonAddr">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmUserAddrForm.commonAddr" placeholder="请输入是否默认地址 1是"/>
            </FormItem>
            <FormItem label="建立时间" prop="createTime">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmUserAddrForm.createTime" placeholder="请输入建立时间"/>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="text" @click="show=false">取消</Button>
            <Button type="primary" :loading="loading" @click="handSubmit" v-if="!disabled">提交</Button>
        </div>
    </Modal>
</template>
<script>
import {addHmUserAddr, updateHmUserAddr, getHmUserAddr} from '@/api/hmzd/hmUserAddr'

export default {
    name: "updateHmUserAddr",
    props: {
        value: {
            type: Boolean,
            default: false
        },
        HmUserAddrId: {
            type: String
        },
        modalTitle: {
            type: String
        }
    },
    data() {
        return {
            show: this.value,
            editTitle: this.modalTitle,
            loading: true,
            disabled: false,
            hmUserAddrForm: {
                receiver: '',
                addr: '',
                mobile: '',
                commonAddr: '',
                createTime: '',
            },
            hmUserAddrFormRule: this.getHmUserAddrFormRule()
        }
    },
    methods: {
        handSubmit() {
            this.$refs['hmUserAddrForm'].validate((valid) => {
                this.loading = true;
                if (valid) {
                    if (this.HmUserAddrId != null && this.HmUserAddrId.trim().length > 0) {
                        this.hmUserAddrForm.id = this.HmUserAddrId;
                        updateHmUserAddr(this.hmUserAddrForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    } else {
                        addHmUserAddr(this.hmUserAddrForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    }
                } else {
                    this.loading = false;
                    this.$Message.error('表单验证不通过！');
                }
            });
        },
        closeModal(val) {
            this.$emit('input', val);
        },
        initForm() {
            this.hmUserAddrForm = {
                receiver: '',
                addr: '',
                mobile: '',
                commonAddr: '',
                createTime: '',
            };
        },
        getHmUserAddrFormRule() {
            return {
                receiver: [
                    {required: true, message: '姓名不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                addr: [
                    {required: true, message: '详细地址不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                mobile: [
                    {required: true, message: '手机不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                commonAddr: [
                    {required: true, message: '是否默认地址 1是不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                createTime: [
                    {required: true, message: '建立时间不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
            }
        }
    },
    watch: {
        value(val) {
            this.show = val;
        },
        show(val) {
            this.initForm();
            this.loading = false;
            this.editTitle = this.modalTitle;
            if (this.editTitle == "查看") {
                this.disabled = true;
            } else {
                this.disabled = false;
            }
            if (val) {
                this.$refs['hmUserAddrForm'].resetFields();
                this.$refs['hmUserAddrForm'].id = null;
                if (this.HmUserAddrId != null && this.HmUserAddrId.trim().length > 0) {
                    getHmUserAddr({id: this.HmUserAddrId}).then(res => {
                        if (res.success) {
                            this.hmUserAddrForm = res.data;
                        } else {
                            this.$Message.error(res.msg);
                        }
                    });
                }
            } else {
                this.closeModal(val);
            }
        }
    }
}
</script>
